package com.atguigu.auth.Service.impl;

import com.atguigu.auth.Service.SysRoleService;
import com.atguigu.auth.Service.SysUserRoleService;
import com.atguigu.auth.mapper.SysRoleMapper;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.system.SysUserRole;
import com.atguigu.vo.system.AssginRoleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {
@Autowired
private SysUserRoleService sysUserRoleService;

//    @Override
//    public Map<String, Object> findRoleByAdminId(Long userId) {
//        return null;
//    }

    @Override
    public void doAssign(AssginRoleVo assginRoleVo) {
//        把用户之前分配的角色数据删除
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId, assginRoleVo.getUserId());
        sysUserRoleService.remove(wrapper);
//        重新进行分配
        List<Long> roleIdList = assginRoleVo.getRoleIdList();
        for (Long roleId : roleIdList) {
            if (StringUtils.isEmpty(roleId)) {
                continue;
            }
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(assginRoleVo.getUserId());
            sysUserRole.setRoleId(roleId);
            sysUserRoleService.save(sysUserRole);
        }
    }

    @Override
    public Map<String, Object> findRoleDataByUserId(Long userId) {
//        查询所有角色  list集合    返回
        List<SysRole> allRoleList = baseMapper.selectList(null);

//        根据userid查询角色关系表，查询用户id对 应的所有的角色id
        LambdaQueryWrapper<SysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUserRole::getUserId, userId);
        List<SysUserRole> existUserRoleList = sysUserRoleService.list();
//        从查询出的用户id对应角色的list集合，获取所有角色的id
    /*    List<Long> list = new ArrayList<>();
        for (SysUserRole sysUserRole:existUserRoleList) {
            Long roleId = sysUserRole.getRoleId();
            list.add(roleId);
        }与下面相等*/

        List<Long> existRoleIdList =
                existUserRoleList.stream().map(c -> c.getRoleId()).collect(Collectors.toList());
//        根据查询所有角色id，找到对应的角色信息
//        根据角色id到所有角色的list集合进行比较
    List<SysRole> assignRoleList = new ArrayList<>();
    for (SysRole sysRole : allRoleList) {
//        比较
        if (existRoleIdList.contains(sysRole.getId())) {
            assignRoleList.add(sysRole);
        }
    }

//        把得到的两部分数据封装到map集合，返回
        Map<String, Object> rolemap = new HashMap<>();
        rolemap.put("assignRoleList", assignRoleList);
        rolemap.put("allRoleList", allRoleList);
        return rolemap;
    }
}
